package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.Attendance;

/**
 * @author 袁小弘
 * @describe 考勤DAO层
 * @date 2019-12-31
 */
@Repository
public interface AttendanceDao extends JpaRepository<Attendance, Integer> {

    @Transactional
    @Modifying
    int deleteByAttendanceId(Integer attendanceId);


    @Transactional
    @Modifying
    int deleteByAttendanceIdIn(List<Integer> attendanceIds);


    List<Attendance> findByAttendanceId(Integer attendanceId);

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,a.* \r\n" +
            "from attendance a INNER JOIN users u on u.user_Id = a.user_Id\r\n" +
            "where a.attendanceTime LIKE CONCAT('%',:attendanceTime,'%')")
    List<Map<String, Object>> findByAttendanceTimeLike(@Param("attendanceTime") String attendanceTime);

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,a.* \r\n" +
            "from attendance a INNER JOIN users u on u.user_Id = a.user_Id\r\n" +
            "where a.attendanceStatus LIKE CONCAT('%',:attendanceStatus,'%')")
    List<Map<String, Object>> findByAttendanceStatusLike(@Param("attendanceStatus") String attendanceStatus);


    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,a.* \r\n" +
            "from attendance a INNER JOIN users u on u.user_Id = a.user_Id\r\n" +
            "where u.account LIKE CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,a.* \r\n" +
            "from attendance a INNER JOIN users u on u.user_Id = a.user_Id\r\n" +
            "where u.nickName LIKE CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,a.* from attendance a INNER JOIN users u on u.user_Id = a.user_Id")
    List<Map<String, Object>> queryAllStudentAndAttendanceInfo();

    @Query(nativeQuery = true, value = "SELECT u.account AS userName,u.nickName,a.* \r\n" +
            "FROM attendance a INNER JOIN users u ON u.user_Id = a.user_Id\r\n" +
            "WHERE u.user_Id =:userId")
    List<Map<String, Object>> selectAttendanceByUserId(@Param("userId") Integer userId);


}
